package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CarInfoDetailMapper;
import com.ruoyi.system.domain.CarInfoDetail;
import com.ruoyi.system.service.ICarInfoDetailService;

/**
 * 吊车列Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-09
 */
@Service
public class CarInfoDetailServiceImpl implements ICarInfoDetailService {
	@Autowired
	private CarInfoDetailMapper carInfoDetailMapper;

	/**
	 * 查询吊车列
	 * 
	 * @param id 吊车列主键
	 * @return 吊车列
	 */
	@Override
	public CarInfoDetail selectCarInfoDetailById(Long id) {
		return carInfoDetailMapper.selectCarInfoDetailById(id);
	}

	/**
	 * 查询吊车列列表
	 * 
	 * @param carInfoDetail 吊车列
	 * @return 吊车列
	 */
	@Override
	public List<CarInfoDetail> selectCarInfoDetailList(CarInfoDetail carInfoDetail) {
		return carInfoDetailMapper.selectCarInfoDetailList(carInfoDetail);
	}

	/**
	 * 新增吊车列
	 * 
	 * @param carInfoDetail 吊车列
	 * @return 结果
	 */
	@Override
	public int insertCarInfoDetail(CarInfoDetail carInfoDetail) {
		carInfoDetail.setCreateTime(DateUtils.getNowDate());
		return carInfoDetailMapper.insertCarInfoDetail(carInfoDetail);
	}

	/**
	 * 修改吊车列
	 * 
	 * @param carInfoDetail 吊车列
	 * @return 结果
	 */
	@Override
	public int updateCarInfoDetail(CarInfoDetail carInfoDetail) {
		carInfoDetail.setUpdateTime(DateUtils.getNowDate());
		return carInfoDetailMapper.updateCarInfoDetail(carInfoDetail);
	}

	/**
	 * 批量删除吊车列
	 * 
	 * @param ids 需要删除的吊车列主键
	 * @return 结果
	 */
	@Override
	public int deleteCarInfoDetailByIds(Long[] ids) {
		return carInfoDetailMapper.deleteCarInfoDetailByIds(ids);
	}

	/**
	 * 删除吊车列信息
	 * 
	 * @param id 吊车列主键
	 * @return 结果
	 */
	@Override
	public int deleteCarInfoDetailById(Long id) {
		return carInfoDetailMapper.deleteCarInfoDetailById(id);
	}

	/**
	 * @MethodName: importCarInfo
	 * @Description: TODO
	 * @date: 2024-09-17 16:27:46
	 */
	@Override
	public String importCarInfo(List<CarInfoDetail> carInfoList, boolean updateSupport) {
		if (StringUtils.isNull(carInfoList) || carInfoList.size() == 0) {
			throw new ServiceException("导入用户数据不能为空！");
		}
		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		for (CarInfoDetail carInfo : carInfoList) {
			try {
				// 验证是否存在这个用户
				// SysUser u = userMapper.selectUserByUserName(user.getUserName());
//				if (StringUtils.isNull(u)) {
//					BeanValidators.validateWithException(validator, user);
//					deptService.checkDeptDataScope(user.getDeptId());
//					String password = configService.selectConfigByKey("sys.user.initPassword");
//					user.setPassword(SecurityUtils.encryptPassword(password));
//					user.setCreateBy(operName);
				carInfoDetailMapper.insertCarInfoDetail(carInfo);
				successNum++;
//					successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
//				} else if (isUpdateSupport) {
//					BeanValidators.validateWithException(validator, user);
//					checkUserAllowed(u);
//					checkUserDataScope(u.getUserId());
//					deptService.checkDeptDataScope(user.getDeptId());
//					user.setUserId(u.getUserId());
//					user.setUpdateBy(operName);
//					userMapper.updateUser(user);
//					successNum++;
//					successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
//				} else {
//					failureNum++;
//					failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
//				}
			} catch (Exception e) {
				failureNum++;
				String msg = "<br/>" + failureNum + "、车辆信息导入失败：";
				failureMsg.append(msg + e.getMessage());
			}
		}
		if (failureNum > 0) {
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new ServiceException(failureMsg.toString());
		} else {
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}
}
